Data Structures

Cool, now that I can do some basic math, and string manipulation, I want more!

A data structure is some conatiner (structure) that holds information (data)

Lists

lists are essentially a 'list' of anything you can think of

probably one of the most basic, versitile, and useful data structure you can think of

they just hold stuff in the order you give it


In [ ]:
#  note, you can't use 'list' as a variable, it is a python protected phrase
my_list = []
my_int_list = [1, 3, 5, 7, 10, 11]

print my_list
print my_int_list

Lists do not have to contain the same type


In [ ]:
my_mixed_list = [True, "i got", 3 + 1, "equals", 4, "goats for christmas"]
print my_mixed_list

you can even store lists into variables!


In [ ]:
my_mixed_list = [True, "i got", 3 + 1, "equals", 4, "goats for christmas"]
print "my_mixed_list = ", my_mixed_list

x = my_mixed_list
print "x = ", x

It's all fun and games until you need to get something out of a list

I have everything stored in a list!

But I need something out of it, what good is a list if I can't get something out?


In [ ]:
# consider this: I have a list of data:
patient_data = ["Daniel", "Chen", 25, "male"]

print patient_data

How would I, for example, get only the first name? or the age?

First there are a few things to mention about indexing in python

  • it is zero indexed
    • this means that the first item in in list is position 0 (not 1)
Index 0 1 2 3
Value Daniel Chen 25 male

In [ ]:
patient_data = ["Daniel", "Chen", 25, "male"]

# extract my first name
print "fname = ", patient_data[0]

In [ ]:
# extract the age
patient_data[2]

Lists are what we call an 'iterable', that is something that you can just go one-by-one through

Remember strings? They are also iterables

Getting parts from a string


In [ ]:
string = "Coconut"

# get the first letter
string[0]

# get the second letter
string[1]

# get the first letter from the end
string[-1]

Slicing

What about when you want just a certain part of a string?

lets bulid off the census ID string we had earlier


In [ ]:
# year 2014; CT 255; ID 1
censusID = "2014-255-001"

# what if we just want the year?
# we can slice!
censusID[0:4]

# since it is from the beginning we can also do the following
censusID[:4]

# both results will return a STRING 2014

In [ ]:
censusID[5:8]

censusID[9:]

Excercise

Introduce yourself to 1 neighbor, get their first and last name.

  • Assign the varriable x to equal "My name is your first and last name"
  • Assign the variable y to equal "My neighbor's name is your neighbor's first and last name"

Write an inequality that extracts out the first names, and test to see whether or not they are equal


In [ ]:
x = "My name is Ben DeCoudres"
y = "my neighbor's name is Michelle Hong"

print x[-13:-10] == y[-13:-5]

'''
print x[-13:-1]
print y[-13:-5]
'''

More lists

What else can you do with lists?


In [ ]:
# some list

lerp = [1, 3, 5, 7, 9, 22]

# append
lerp.append(4)
print lerp

In [ ]:
# pop (get the last value, and also remove from list)
print lerp.pop()
print lerp

In [ ]:
# delete second entry
del lerp[1]
print lerp

In [ ]:
# slice (just like strings)

In [ ]:
# add lists

In [ ]:
# check if something is in a list
7 in lerp

In [ ]:
# check how many elements are in a list
len(lerp)

Tuples

Tuples are just like lists, except hwatever you put in there, is permanent, it cannot be changed

you might think that's weird, but an example is after you load in patient data, and you do not want any of the information accidentely changed, you can load it into a tuple to ensure what you put in stays the same


In [ ]:
# use a parenthesis instead of square bracket, everything else is the same
terp = (2, 6, 8, True, "hello")
print terp

Dictionaries

Think of dictionaries like those giant fat books that you don't use anymore

there is a key (the word you want to look up)
and a value (the definition of the word, key)

this key-value pair is what makes a dictionary

features of dictionaries:

Keys

  • Immutable
  • unique
  • not stored an any particular order

Values

  • there are no restrictions
    • values do not have to be unique or immutable

In [ ]:
# start with an empty dictionary, notice dictionaries are enclosed in squiggly brackets
derp = {}

key = "daniel chen"
value = 25

# you fill dictionaries, just like lists

derp = {key:value, "niels bantilan": 25}
print derp

# note dictionaries are not ordered, DO NOT assume any order when working with dictionaries

So we have an initial dictionary with stuff, how do we 'append' things to it and add more values?


In [ ]:
derp["my_new_key"] = "my_new_value"
print derp

In [ ]:
derp["Vivian Peng"] = 25
print derp

In [ ]:
# remove entry from dictionary
del derp["my_new_key"]
print derp

In [ ]:
# assigning a new value to a key that already exists will overwrite the original value
derp["daniel chen"] = 21
print derp

Now that we have a dictionary, how do we 'look' keys up?


In [ ]:
# let's look up how old I am
print derp["daniel chen"]

In [ ]:
# there's another way
print derp.get("daniel chen")

same answer, but lets look at the results if we look for something that does not exist


In [ ]:
print derp["monty python"]

In [ ]:
print derp.get("monty python")

we can also use the 'in' operator to check if something exists in our dictionary


In [ ]:
'monty python' in derp

In [ ]:
'daniel chen' in derp

More dictionary stuff


In [ ]:
# get all keys
print derp.keys()

In [ ]:
#get all values
print derp.values()

In [ ]:
# get the length (number of key:value entries)
print len(derp)

In [ ]:
# check if a key exists in dictionary:
print "daniel chen" in derp

Here's a useful command:

if you ever get confused if your variable is a list, tuple, dictionary, int, float, whatever


In [ ]:
type(derp) # is your best friend!

Excercise

1

Try loading the following key:value pairs into a dictionary

fname:"fname",
lname:"lname",
age:25,
height:69,
weight 180


In [ ]:
my_dict = {}


my_dict = {"fname":"daniel", "lname":"daniel"}


fname = "fname"
lname = "lname"
my_dict = {fname:"daniel", lname:"daniel"}

print my_dict

2

Here's a difficult question: do not worry if you cannot figure this out.

How would you put the fname, lname, age, height, and weight ALL into the same dictionary entry?


In [ ]:
#using list
hardQuestion = {1: ["daniel", "chen", 25, 69]}
print hardQuestion

print hardQuestion.get(1)[0]

# using dictionary
hardQuestion = {1: {"fname":"daniel", "lname":"chen", "age":25, "height":69}}
print hardQuestion

print hardQuestion.get(1).get("fname")

firstPerson =  hardQuestion.get(1)
firstPerson
type(firstPerson)

print firstPerson.get("fname")